import json
from collections import deque

# 数据存储
string_data_store = {}
linked_list_data_store = {}


def load_string_data():
    try:
        with open('../data/data.json', 'r') as f:
            data = json.load(f)
            string_data_store.update(data.get('string_data', {}))
    except FileNotFoundError:
        pass


def save_string_data():
    data = {'string_data': string_data_store}
    with open('../data/data.json', 'w') as f:
        json.dump(data, f)


# 字符串类型指令处理
def handle_set(key, value):
    string_data_store[key] = value
    save_string_data()
    return "1"


def handle_get(key):
    return string_data_store.get(key, "")


def handle_del(key):
    if key in string_data_store:
        del string_data_store[key]
        save_string_data()
        return "1"
    return "0"


# 双向链表类型指令处理
def handle_lpush(key, value):
    if key not in linked_list_data_store:
        linked_list_data_store[key] = deque()
    linked_list_data_store[key].appendleft(value)
    return "1"


def handle_rpush(key, value):
    if key not in linked_list_data_store:
        linked_list_data_store[key] = deque()
    linked_list_data_store[key].append(value)
    return "1"


def handle_range(key, start, end):
    if key in linked_list_data_store:
        result = list(linked_list_data_store[key])[start: end + 1]
        return " ".join(result)
    return ""


def handle_len(key):
    if key in linked_list_data_store:
        return str(len(linked_list_data_store[key]))
    return "0"


def handle_lpop(key):
    if key in linked_list_data_store and linked_list_data_store[key]:
        return str(linked_list_data_store[key].popleft())
    return ""


def handle_rpop(key):
    if key in linked_list_data_store and linked_list_data_store[key]:
        return str(linked_list_data_store[key].pop())
    return ""


def handle_ldel(key):
    if key in linked_list_data_store:
        del linked_list_data_store[key]
        return "1"
    return "0"


# 其他指令处理
def handle_ping():
    return "PONG"


def handle_help(command=None):
    if command:
        if command in ["set", "get", "del", "lpush", "rpush", "range", "len", "lpop", "rpop", "ldel", "ping", "help"]:
            return f"Usage for {command}"
        else:
            return "Invalid command"
    else:
        return "Available commands: set, get, del, lpush, rpush, range, len, lpop, rpop, ldel, ping, help"


def handle_command(command_str):
    parts = command_str.split()
    command = parts[0]
    if command == "set":
        return handle_set(parts[1], parts[2])
    elif command == "get":
        return handle_get(parts[1])
    elif command == "del":
        return handle_del(parts[1])
    elif command == "lpush":
        return handle_lpush(parts[1], parts[2])
    elif command == "rpush":
        return handle_rpush(parts[1], parts[2])
    elif command == "range":
        return handle_range(parts[1], int(parts[2]), int(parts[3]))
    elif command == "len":
        return handle_len(parts[1])
    elif command == "lpop":
        return handle_lpop(parts[1])
    elif command == "rpop":
        return handle_rpop(parts[1])
    elif command == "ldel":
        return handle_ldel(parts[1])
    elif command == "ping":
        return handle_ping()
    elif command == "help":
        if len(parts) > 1:
            return handle_help(parts[1])
        else:
            return handle_help()
    else:
        return "Invalid command"


load_string_data()